package com.chhh.cranelease.service.impl;

import com.chhh.cranelease.entity.User;
import com.chhh.cranelease.mapper.UserMapper;
import com.chhh.cranelease.model.UserWithRole;
import com.chhh.cranelease.service.UserService;
import com.chhh.cranelease.util.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserWithRole selectByLoginAcct(User user) {
        String md5Userpswd = Md5.md5(user.getUserPswd());
        user.setUserPswd(md5Userpswd);
       return userMapper.selectByLoginAcct(user);
    }

    @Override
    public Integer updateInfo(User user) {
        user.setUserPswd(Md5.md5(user.getUserPswd()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(sdf.format(new Date()));
        return userMapper.updateByPrimaryKeySelective(user);

    }

    @Override
    public boolean selecLoginAcct(String loginAcct) {

        UserWithRole user =  userMapper.selectLoginAcct(loginAcct);
        System.out.println(user);
        if (user == null){
            return false;
        }
        return true;
    }

    @Override
    public void insertUser(User user) {
        user.setUserPswd(Md5.md5(user.getUserPswd()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(sdf.format(new Date()));
        userMapper.insertSelective(user);
    }
}
